home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / w00w00 / sectools / fragrouter / Libnet-0.99b / src / pf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-07-26  |  4.4 KB  |  158 lines

  1. /*
  2.  *  $Id: pf.c,v 1.1.1.1 1999/05/18 15:33:42 dugsong Exp $
  3.  *
  4.  *  libnet
  5.  *  pf.c
  6.  *
  7.  *  Copyright (c) 1998, 1999 Mike D. Schiffman <mike@infonexus.com>
  8.  *                           route|daemon9 <route@infonexus.com>
  9.  *  All rights reserved.
  10.  *
  11.  * Copyright (c) 1990, 1991, 1992, 1993, 1994, 1995, 1996
  12.  *    The Regents of the University of California.  All rights reserved.
  13.  *
  14.  * Redistribution and use in source and binary forms, with or without
  15.  * modification, are permitted provided that: (1) source code distributions
  16.  * retain the above copyright notice and this paragraph in its entirety, (2)
  17.  * distributions including binary code include the above copyright notice and
  18.  * this paragraph in its entirety in the documentation or other materials
  19.  * provided with the distribution, and (3) all advertising materials mentioning
  20.  * features or use of this software display the following acknowledgement:
  21.  * ``This product includes software developed by the University of California,
  22.  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
  23.  * the University nor the names of its contributors may be used to endorse
  24.  * or promote products derived from this software without specific prior
  25.  * written permission.
  26.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  27.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  28.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  29.  *
  30.  * packet filter subroutines for tcpdump
  31.  *    Extraction/creation by Jeffrey Mogul, DECWRL
  32.  */
  33.  
  34. #if (HAVE_CONFIG_H)
  35. #include "../include/config.h"
  36. #endif
  37. #include "../include/low_libnet.h"
  38.  
  39. #include "../include/gnuc.h"
  40. #ifdef HAVE_OS_PROTO_H
  41. #include "../include/os-proto.h"
  42. #endif
  43.  
  44. struct link_int *
  45. open_link_interface(char *device, char *ebuf)
  46. {
  47.     register struct link_int *l;
  48.     short enmode;
  49.     int backlog = -1;   /* request the most */
  50.     struct enfilter Filter;
  51.     struct endevp devparams;
  52.  
  53.     l = (struct link_int *)malloc(sizeof(*l));
  54.     if (l == NULL)
  55.     {
  56.         sprintf(ebuf, "libnet_open_link_int: %s", ll_strerror(errno));
  57.         return (0);
  58.     }
  59.     memset(l, 0, sizeof(*l));
  60.     l->fd = pfopen(device, O_RDWR);
  61.     if (l->fd < 0)
  62.     {
  63.         sprintf(ebuf, "pf open: %s: %s\n\your system may not be properly configured; see \"man packetfilter(4)\"\n",
  64.             device, ll_strerror(errno));
  65.         goto bad;
  66.     }
  67.  
  68.     enmode = ENTSTAMP|ENBATCH|ENNONEXCL;
  69.     if (ioctl(l->fd, EIOCMBIS, (caddr_t)&enmode) < 0)
  70.     {
  71.         sprintf(ebuf, "EIOCMBIS: %s", ll_strerror(errno));
  72.         goto bad;
  73.     }
  74. #ifdef    ENCOPYALL
  75.     /* Try to set COPYALL mode so that we see packets to ourself */
  76.     enmode = ENCOPYALL;
  77.     ioctl(l->fd, EIOCMBIS, (caddr_t)&enmode);   /* OK if this fails */
  78. #endif
  79.     /* set the backlog */
  80.     if (ioctl(l->fd, EIOCSETW, (caddr_t)&backlog) < 0)
  81.     {
  82.         sprintf(ebuf, "EIOCSETW: %s", ll_strerror(errno));
  83.         goto bad;
  84.     }
  85.     /*
  86.      *  discover interface type
  87.      */
  88.     if (ioctl(l->fd, EIOCDEVP, (caddr_t)&devparams) < 0)
  89.     {
  90.         sprintf(ebuf, "EIOCDEVP: %s", ll_strerror(errno));
  91.         goto bad;
  92.     }
  93.  
  94.     /* HACK: to compile prior to Ultrix 4.2 */
  95. #ifndef    ENDT_FDDI
  96. #define    ENDT_FDDI   4
  97. #endif
  98.     switch (devparams.end_dev_type)
  99.     {
  100.         case ENDT_10MB:
  101.             l->linktype = DLT_EN10MB;
  102.             break;
  103.         case ENDT_FDDI:
  104.             l->linktype = DLT_FDDI;
  105.             break;
  106.         default:
  107.             /*
  108.              * XXX
  109.              * Currently, the Ultrix packet filter supports only
  110.              * Ethernet and FDDI.  Eventually, support for SLIP and PPP
  111.              * (and possibly others: T1?) should be added.
  112.              */
  113.             l->linktype = DLT_EN10MB;
  114.             break;
  115.     }
  116.     /*
  117.      *  accept all packets
  118.      */
  119.     bzero((char *)&Filter, sizeof(Filter));
  120.     Filter.enf_Priority = 37;    /* anything > 2 */
  121.     Filter.enf_FilterLen = 0;    /* means "always true" */
  122.     if (ioctl(l->fd, EIOCSETF, (caddr_t)&Filter) < 0)
  123.     {
  124.         sprintf(ebuf, "EIOCSETF: %s", ll_strerror(errno));
  125.         goto bad;
  126.     }
  127.  
  128.     return (l);
  129. bad:
  130.     free(l);
  131.     return (NULL);
  132. }
  133.  
  134.  
  135. int
  136. close_link_interface(struct link_int *l)
  137. {
  138.     return (close(l->fd));
  139. }
  140.  
  141.  
  142. int
  143. write_link_layer(struct link_int *l, const u_char *device, const u_char *buf,
  144.             int len)
  145. {
  146.     int c;
  147.  
  148.     c = write(l->fd, buf, len);
  149.     if (c != len)
  150.     {
  151. #if (__DEBUG)
  152.         fprintf(stderr, "write_link_layer: %d bytes written (%s)\n", c,
  153.             strerror(errno));
  154. #endif
  155.     }
  156.     return (c);
  157. }
  158.